//@reduxjs/toolkit

import {createSlice,configureStore,createAsyncThunk} from "@reduxjs/toolkit"


let msgSlice = createSlice({
    name:"msgSlice",
    //存储状态
    initialState:{
        msg:"我是初始值reduxtoolkit"
    },
    //此处相当于方法
    reducers:{
        changeMsg(state,action){
            state.msg = action.payload
        }
    }
})

//创建异步切片并暴露出去

export let asyncChangeNum = createAsyncThunk('asyncChangeNum',async(params)=>{
    console.log(params)
    let res = await new Promise((resolve)=>{
        setTimeout(()=>{
           resolve(params)
        },2000)
    })
    
    return res
})

let numSlice = createSlice({
    name:"numSlice",
    initialState:{
        num:0
    },
    reducers:{
        addNum(state){
            state.num += 1
        }
    },
    // extraReducers:(chunk)=>{
    //     chunk
    //     .addCase(asyncChangeNum.fulfilled,(state,action)=>{
    //         state.num  = action.payload
    //     })
    // }
    extraReducers:{
        [asyncChangeNum.fulfilled]:(state,action)=>{
            state.num  = action.payload
        }
    }
})
// console.log(msgSlice)

export let {changeMsg} = msgSlice.actions
export let {addNum} = numSlice.actions
let store = configureStore({
    reducer:{
        msgReducer:msgSlice.reducer,
        numReducer:numSlice.reducer
    }
})


export default store
